package com.atguigu.gmall.util;


import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Map;

public class JwtUtil {
    public static String encode(String key, Map<String,Object> param, String salt){
        if(salt != null){
            key+=salt;
        }
        JwtBuilder jwtBuilder = Jwts.builder().signWith(SignatureAlgorithm.HS256, key);
        jwtBuilder = jwtBuilder.setClaims(param);
        String token = jwtBuilder.compact();
        return token;
    }
    public static Map<String,Object> decode(String token,String key,String salt){
        Claims claims = null;
        if(salt != null){
            key += salt;
        }
        try{
            claims = Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody();
        }catch (Exception e){
            return null;
        }

        return claims;
    }
}
